package io.onedev.server.security.permission;

import org.apache.shiro.authz.Permission;

import io.onedev.commons.utils.match.StringMatcher;
import io.onedev.server.util.patternset.PatternSet;

public class JobPermission implements Permission {

	private final String jobNames;
	
	private final Permission privilege;
	
	public JobPermission(String jobNames, Permission privilege) {
		this.jobNames = jobNames;
		this.privilege = privilege;
	}
	
	@Override
	public boolean implies(Permission p) {
		if (p instanceof JobPermission) {
			JobPermission jobPermission = (JobPermission) p;
			return PatternSet.fromString(jobNames).matches(new StringMatcher(), jobPermission.jobNames) 
					&& privilege.implies(jobPermission.privilege);
		} 
		return false;
	}

}
